﻿using System;
using System.Threading;

namespace 事件
{
    class Program
    {
        static void Main(string[] args)
        {
			var test = new Heater();
			test.OnBoiled += TestOnBoiled;
			test.OnBoiled += TestOnBoiled;
			test.Begin();
			Console.ReadKey();
		}

		static void TestOnBoiled(object sender, EventArgs e)
		{
			Console.WriteLine("Hello事件被调用");
		}
	}



	/// 
	/// 热水器
	/// 
	public class Heater
	{
		public event EventHandler OnBoiled;
		private void RasieBoiledEvent()
		{
			if (OnBoiled == null)
			{
				Console.WriteLine("加热完成处理订阅事件为空");
			}
			else
			{
				OnBoiled(this, new EventArgs());
			}
		}
		private Thread heatThread;
		public void Begin()
		{
			heatTime = 5;
			heatThread = new Thread(new ThreadStart(Heat));
			heatThread.Start();
			Console.WriteLine("加热器已经开启", heatTime);

		}
		private int heatTime;
		private void Heat()
		{
			while (true)
			{
				Console.WriteLine("加热还需{0}秒", heatTime);

				if (heatTime == 0)
				{
					RasieBoiledEvent();
					return;
				}
				heatTime--;
				Thread.Sleep(1000);

			}
		}
	}
}